package com.chixing.controller;

import com.chixing.pojo.Myorder;
import com.chixing.service.MyorderService;

import com.chixing.service.impl.MsgSender;
import com.chixing.util.DateUtil;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
public class MyorderController {
    @Autowired
    private MyorderService myorderService;

    @Autowired
    private MsgSender msgSender;


    //根据订单id获取单个订单的详情
    @GetMapping("myOrder/{orderId}")
//    @ResponseBody
    public ModelAndView getByOrderId(@PathVariable("orderId") Integer orderId){
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = myorderService.getByOrderId(orderId);
        System.out.println(serverResponse);
        mav.addObject("serverResponse",serverResponse);
        mav.setViewName("myorder/order_details");
        return mav;
    }

    //根据订单No获取订单详情
    @GetMapping("myorder/{orderNo}")
    @ResponseBody
    public ModelAndView getByOrderNo(@PathVariable("orderNo") String orderNo){
        ModelAndView mav = new ModelAndView();
        ServerResponse serverResponse = myorderService.getByOrderNo(orderNo);
        mav.addObject("serverResponse",serverResponse);
        mav.setViewName("myorder/order_details");
        return mav;
    }


    //订单查询
    @GetMapping(value = {"order/customer/{custId}/{myorderStatus}","order/customer/{custId}"})
    @ResponseBody
    public ServerResponse getByCustIdAndMyorderStatus(@PathVariable("custId") Integer custId,
                                                    @PathVariable(value = "myorderStatus",required = false) Integer myorderStatus){
        System.out.println("custId: " + custId);
        System.out.println("myorderStatus: " + myorderStatus);
        if (myorderStatus == null)
            myorderStatus = 5;
        ServerResponse serverResponse = myorderService.getByCustId(custId, myorderStatus);
        return serverResponse;
    }

    //添加订单
    @PostMapping("save")
    @ResponseBody
    public Myorder createNewOrder(String custName , Long custTel , String travelName, Integer itineraryId,
                                  Integer audltNum, Integer childNum, String myorderDate, Float myorderPrice) {
        System.out.println("1111111111111111111111111111111111111");
        System.out.println("custName:" + custName);
        System.out.println("custTel:" + custTel);
        System.out.println("travelName:" + travelName);
        System.out.println("myorderDate:" + myorderDate);
        System.out.println("myorderPrice:" + myorderPrice);
        if (itineraryId == null)
            itineraryId = null;
        Date date = DateUtil.string2Date(myorderDate, DateUtil.DATE_PATTERN_YMD);
        Myorder myorder = myorderService.createOrder(custName, custTel, travelName, itineraryId, audltNum, childNum, null, date, myorderPrice);
        System.out.println(myorder);
        msgSender.addVipOrderMsg(myorder,1800);
        return myorder;
    }

//    @PutMapping("myOrder/{orderId}/update")
//    public String updateMyorder(@PathVariable("orderId") Integer orderId, Integer orderStatus, HttpServletRequest request){
//        Customer customer = (Customer) request.getSession().getAttribute("loginCustomer");
//        boolean is = myorderService.updateByMyorderId(orderId, orderStatus);
//        Integer custId = customer.getCustId();
////        return "order/customer/" +custId + "/" + orderStatus;
//
//        return "redirect:/myorder/myorder_center.jsp";
//    }

    //删除订单
    @PutMapping("delete/{myorderNo}")
    @ResponseBody
    public ModelAndView deleteMyorder(@PathVariable("myorderNo") String myorderNo){
        System.out.println("删除删除删除");
        ModelAndView mav = new ModelAndView("myorder/myorder_center");
        myorderService.deleteMyorder(myorderNo);
        return mav;
    }

    //生成流水
    public boolean createFlow(String myorderNo){
        return myorderService.createFlow(myorderNo);
    }

    //订单取消
    @PutMapping("cancel/{myorderNo}")
//    @ResponseBody
    public String cancelMyorder(@PathVariable("myorderNo") String myorderNo){
        System.out.println("10101001010101010");
        myorderService.cancelMyorder(myorderNo);
        //订单状态更改,跳往订单详情页面
        return "redirect:/myorder/" + myorderNo;
    }

    // 根据日期获取票数
    @GetMapping("date/tickets/{sightsId}")
    @ResponseBody
    public ServerResponse getDayTickets(@RequestParam Date date,@PathVariable("sightsId") Integer sightId){
        return myorderService.getDayTickets(date,sightId);
    }


    //添加订单评论
//    @PostMapping("myorder/{myorderNo}/{content}")
    @PostMapping("myorder/review")
    @ResponseBody
    public boolean addMyorderReview(String myorderNo,String content){
        return myorderService.addMyorderReview(myorderNo,content);
    }
}

